Current Location: Home> Function Categories> is_a

is_a

Check if an object belongs to this class, or to use this class as one of its parent classes: Return true if the object belongs to that class or is the parent class of this object
Name:is_a
Category:Classes and Objects
Programming Language:php
One-line Description:Check whether an object belongs to the specified class or its subclass

Function name: is_a()

Applicable version: PHP 4, PHP 5, PHP 7

Usage: The is_a() function is used to check whether an object belongs to the specified class or its subclass.

Syntax: bool is_a( object $object, string $class_name )

parameter:

  • $object: The object to be checked.
  • $class_name: The class name to be checked.

Return value:

  • Return true if $object is an object of $class_name or an object of a subclass of $class_name.
  • If $object is not an object of $class_name or an object of a subclass of $class_name, false is returned.

Example:

 class Person { public $name; } class Student extends Person { public $grade; } $person = new Person(); $student = new Student(); // 检查$person 是否是Person 类的对象if (is_a($person, 'Person')) { echo '$person 是Person 类的对象'; } else { echo '$person 不是Person 类的对象'; } // 检查$student 是否是Person 类的对象if (is_a($student, 'Person')) { echo '$student 是Person 类的对象'; } else { echo '$student 不是Person 类的对象'; } // 检查$student 是否是Student 类的对象if (is_a($student, 'Student')) { echo '$student 是Student 类的对象'; } else { echo '$student 不是Student 类的对象'; }

Output:

 $person 是Person 类的对象$student 是Person 类的对象$student 是Student 类的对象

In the above example, we define a Person class and a Student class, which is a subclass of Person class. We create a $person object and a $student object. Use the is_a() function to check the class relationship of these objects. The first check shows that $person is an object of the Person class, the second check shows that $student is also an object of the Person class, and the third check shows that $student is an object of the Student class.